<?php
/*******************************************************************************
 *
 *  Controller Name: CR_form   
 *
 *  Date: 2012-04-27
 *  
 *  Description: The main controller for all the customer service activities
 *  
 *  Author: Travis Rous
 *  
 *  Todo:
 *     NONE
 *
 *  Modification History:  
 * 
 *   Date         Name
 *  ----------   --------------
 *  2012-04-26   TRAVIS ROUS
 *  2012-04-26   TRAVIS ROUS
 *  2012-04-25   TRAVIS ROUS
 *  2012-04-23   TRAVIS ROUS
 *  2012-04-23   TRAVIS ROUS
 *  2012-04-22   TRAVIS ROUS
 *  2012-04-17   TRAVIS ROUS
 * 
 *****************************************************************************/
?>
<?php



class CR_form extends CI_Controller {

	/*******************************************************************************
	 *
	 *  Function Name:   construct
	 *
	 *  Date: 2012-04-27
	 *  
	 *  Description: gets user data if not a CR then redirect them to login
	 *  
	 *  Author: Travis Rous
	 *****************************************************************************/
   public function __construct()
   {
      parent::__construct(); //Manually call parent contructor
      $this->load->library('session');
      $this->load->helper(array('form','url'));
	  //load the customer rep model to get user info
      $this->load->model('Customerrep_model','CustomerRep');
      $current_user = $this->session->all_userdata();
	  //check if user is a customer rep
      if(array_key_exists('UserID',$current_user))
      {
         $result = $this->CustomerRep->get($current_user["UserID"]);
         if( count( $result ) == 0)
            redirect('/user/login');
      }
      else 
         redirect('/user/login');
   }

	function index()//index fuction for the CI stuff
	{
	}
	/*******************************************************************************
	 *
	 *  Function Name:   changeReport
	 *
	 *  Date: 2012-04-27
	 *  
	 *  Description: loads the edit report page if it is a sensor then it loads 
					 sensor edit page if it is a customer then it loads the
					 customer edit page
	 *  
	 *	Parameters:  $ReportID is needed to find the correct report to change
	 *
	 *  Author: Travis Rous
	 *****************************************************************************/
	public function changeReport($ReportID)
	{
	    //Load header of viewincid
        $this->load->helper('dashboard_helper');
        $data['menu'] = getMenu();
		$this->load->helper(array('form', 'url'));
		$this->load->view('headerview',$data);
		
		
		//load the report model to read in the reports into the next view
		$this->load->model('Report_model','report');
       $reports = $this->report->getWithCustomerInfo("WHERE Report.ReportID = $ReportID");
        $data['Report'] = $reports[0];
        $buff = $data['Report'];
        if ($buff['Sensor'] == 1)
        {    //takes us to the sensor editing page
            $this->load->view('changeReport',$data);
	    }
	    else
	    {	//takes us to the customer editing page
	        $this->load->view('change_Customer',$data);
	    }
	}
	
	/*******************************************************************************
	 *
	 *  Function Name:   edit_sensor
	 *
	 *  Date: 2012-04-27
	 *  
	 *  Description: Loads the edit customer page to allow someone to change report
	 *  
	 *  Author: Travis Rous
	 *****************************************************************************/
	public function edit_customer()
	{
	    //Load header of viewincid
        $this->load->helper('dashboard_helper');
        $data['menu'] = getMenu();
		$this->load->helper(array('form', 'url'));
		$this->load->view('headerview',$data);
		
		$this->load->helper(array('form', 'url'));
		//form validation
		$this->load->library('form_validation');
		$this->form_validation->set_rules('ReportID', 'Report ID', 'required');
		$this->form_validation->set_rules('firstname', 'First Name', 'required|max_length[50]');
		$this->form_validation->set_rules('lastname', 'Last Name', 'required|max_length[50]');
		$this->form_validation->set_rules('Addressline1', 'Address Line', 'required');
		$this->form_validation->set_rules('city', 'City', 'required');
		$this->form_validation->set_rules('State', 'State', 'required');
		$this->form_validation->set_rules('zipcode', 'Zip Code', 'required|integer|min_length[5]|max_length[5]');
		$this->form_validation->set_rules('email', 'E-mail', 'required|valid_email');
		$this->form_validation->set_rules('phonenum', 'Phone Number', 'required|integer|min_length[10]|max_length[10]');
		$this->form_validation->set_rules('Comments', 'Comments', 'optional');
		if ($this->form_validation->run() == FALSE)
		{
			//change to the secondary edit screen if there were form errors
			$this->load->view('change_Customer2');
		}
		else
		{
		
        $current_user = $this->session->all_userdata();
        
        //load functions to get the date for a timestamp of the edit
		$this->load->helper('date');
		$datestring = "%Y-%m-%d %H:%i:%s";
		$time = time();
		//format the date
		$form_time = mdate($datestring, $time);
		//get the form of a customer model
		$this->load->model('CustomerInfo_model','customerInfo');
		$submission1 = $this->customerInfo->get($this->input->post('ReportID', TRUE));
		$submission1 = $submission1[0];
		
		//update the customer model
		$submission1['Firstname'] = $this->input->post('firstname', TRUE);
		$submission1['Lastname'] = $this->input->post('lastname', TRUE);
		$submission1['PhoneNumber'] = $this->input->post('phonenum', TRUE);
		$submission1['Email'] = $this->input->post('email', TRUE);
		$this->customerInfo->update($submission1);
		//---------------------------------------------------------------
		//get the form of the rest of the report information
		$this->load->model('Report_model','report');
		$submission = $this->report->get($this->input->post('ReportID', TRUE));
		$submission = $submission[0];
		
		//update the rest of the information
		$submission['State'] = $this->input->post('State', TRUE);
		$submission['description'] = $this->input->post('Comments', TRUE);
		$submission['City'] = $this->input->post('city', TRUE);
		$submission['Address'] = $this->input->post('Addressline1', TRUE);
		$submission['SensorID'] = $this->input->post('sensorID', TRUE);
		$submission['CustomerRepID'] = $current_user['UserID'];
		$submission['ReportTime'] = mdate($datestring, $time);
		$submission['zipcode'] = $this->input->post('zipcode', TRUE);
		
        $this->report->update($submission);
		 //retrun to the view reports page with success message
		 $this->session->set_flashdata('message','Report successfully edited');
         redirect('CR_form/viewMyReports/20');

		}
	}
	/*******************************************************************************
	 *
	 *  Function Name:   edit_sensor
	 *
	 *  Date: 2012-04-27
	 *  
	 *  Description: Loads the edit sensor page to allow someone to change report
	 *  
	 *  Author: Travis Rous
	 *****************************************************************************/
	public function edit_sensor()
	{
	    //Load header of viewincid
        $this->load->helper('dashboard_helper');
        $data['menu'] = getMenu();
		$this->load->helper(array('form', 'url'));
		$this->load->view('headerview',$data);
		
		$this->load->helper(array('form', 'url'));

		//form validation
		$this->load->library('form_validation');
		$this->form_validation->set_rules('ReportID', 'Report ID', 'required');
		$this->form_validation->set_rules('sensorID', 'Sensor ID', 'required|integer');
		$this->form_validation->set_rules('Addressline1', 'Address Line', 'required');
		$this->form_validation->set_rules('city', 'City', 'required');
		$this->form_validation->set_rules('State', 'State', 'required');
		$this->form_validation->set_rules('zipcode', 'Zip Code', 'required|integer|min_length[5]|max_length[5]');
		$this->form_validation->set_rules('Comments', 'Comments', 'optional');
		if ($this->form_validation->run() == FALSE)
		{
			//if there are errors to in the report that was edited load seconday view
			$this->load->view('changeReport2');
		}
		else
		{
        $current_user = $this->session->all_userdata();
        $this->load->model('Report_model','report');
		
         //load time functions
		$this->load->helper('date');
		$datestring = "%Y-%m-%d %H:%i:%s";
		$time = time();
		//update the report with the new information
		$submission = $this->report->get($this->input->post('ReportID', TRUE));
		$submission = $submission[0];
		$form_time = mdate($datestring, $time);
		$submission['State'] = $this->input->post('State', TRUE);
		$submission['description'] = $this->input->post('Comments', TRUE);
		$submission['City'] = $this->input->post('city', TRUE);
		$submission['Address'] = $this->input->post('Addressline1', TRUE);
		$submission['SensorID'] = $this->input->post('sensorID', TRUE);
		$submission['CustomerRepID'] = $current_user['UserID'];
		$submission['ReportTime'] = mdate($datestring, $time);
		$submission['zipcode'] = $this->input->post('zipcode', TRUE);

         $this->report->update($submission);
		 //return to the view reports page with success message
		 $this->session->set_flashdata('message','Report successfully edited');
         redirect('CR_form/viewMyReports/20');

		}
	}
	/*******************************************************************************
	 *
	 *  Function Name:   viewReport
	 *
	 *  Date: 2012-04-27
	 *  
	 *  Description: used to view a single report in detail
	 *
	 *  Parameters:  $reportID is needed to find the report that is being displayed
	 *  
	 *  Author: Travis Rous
	 *****************************************************************************/
    public function viewReport($ReportID)
   {
	    //Load header of viewincid
        $this->load->helper('dashboard_helper');
        $data['menu'] =getMenu();
		$this->load->helper(array('form', 'url'));
		$this->load->view('headerview',$data);
		
		//loads the model for a report
       $this->load->model('Report_model','report');
	   //creates an array with all report info with the given ID
       $reports = $this->report->getWithCustomerInfo("WHERE Report.ReportID = $ReportID");
       $data['Report'] = $reports[0];
	   //loads the View Reports page
       $this->load->view('ViewMyReport',$data);

   }
   /*******************************************************************************
	 *
	 *  Function Name:   viewMyReports
	 *
	 *  Date: 2012-04-27
	 *  
	 *  Description: used to view only the reports made by the user that is logged in
	 *
	 *  Parameters:  $num is the number of reports that are to display(default = 20)
	 *  
	 *  Author: Travis Rous
	 *****************************************************************************/
   public function viewMyReports($num)
   {
	    //Load header of viewincid
        $this->load->helper('dashboard_helper');
        $data['menu'] = getMenu();
		$this->load->helper(array('form', 'url'));
		$this->load->view('headerview',$data);
		
		//load the report form
		$this->load->model('Report_model','report');
		 //make an array of things to list in the table of reports
         $results = $this->report->getall();
         $data["entries"] = $results;
         $data["title"]= "My reports";
         $data["num"] = $num;
      $this->load->view('ViewMyReports', $data); 

   }
   /*******************************************************************************
	 *
	 *  Function Name:   input
	 *
	 *  Date: 2012-04-27
	 *  
	 *  Description: used to create a customer report
	 *  
	 *  Author: Travis Rous
	 *****************************************************************************/
	public function input()
	{
	    //Load header of viewincid
        $this->load->helper('dashboard_helper');
        $data['menu'] = getMenu();
		$this->load->helper(array('form', 'url'));
		$this->load->view('headerview',$data);

		//for validation
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('firstname', 'First Name', 'required|max_length[50]');
		$this->form_validation->set_rules('lastname', 'Last Name', 'required|max_length[50]');
		$this->form_validation->set_rules('Addressline1', 'Address Line', 'required');
		$this->form_validation->set_rules('city', 'City', 'required');
		$this->form_validation->set_rules('State', 'State', 'required');
		$this->form_validation->set_rules('zipcode', 'Zip Code', 'required|integer|min_length[5]|max_length[5]');
		$this->form_validation->set_rules('email', 'E-mail', 'required|valid_email');
		$this->form_validation->set_rules('phonenum', 'Phone Number', 'required|integer|min_length[10]|max_length[10]');
		$this->form_validation->set_rules('Comments', 'Comments', 'optional');
		if ($this->form_validation->run() == FALSE)
		{
			//reload the page to show the failed inputs
			$this->load->view('Proto_page');
		}
		else
		{
		$current_user = $this->session->all_userdata();
        $this->load->model('Report_model','report');
		//load date function to format time and date
		$this->load->helper('date');
		$datestring = "%Y-%m-%d %H:%i:%s";
		$time = time();
		$form_time = mdate($datestring, $time);
		//create the array to submit
         $submission = array( 
			'City' => $this->input->post('city', TRUE),
			'Zipcode' => $this->input->post('zipcode', TRUE),
			'Address' => $this->input->post('Addressline1', TRUE),
			'State' => $this->input->post('State', TRUE),
			'Description' => $this->input->post('Comments', TRUE),
			'CustomerRepID' => $current_user['UserID'],
			'ReportTime' => mdate($datestring, $time),
			'Sensor' => 0
            );
		 //use array to make report
         $this->report->insert($submission);
		 $id = $this->report->findID($form_time);
		 
		 //load the customer info form
		 $this->load->model('Customerinfo_model','CustomerInfo');
		 $submission2 = array(
			'ReportID' => $id,
			'PhoneNumber' => $this->input->post('phonenum', TRUE),
			'Firstname' => $this->input->post('firstname', TRUE),
			'Lastname' => $this->input->post('lastname', TRUE),
			'Email' => $this->input->post('email', TRUE)
		 );
		 //make the customer info part of the report
		 $this->CustomerInfo->insert($submission2);
         //Set success message and return to the view reports page
         $this->session->set_flashdata('message','Report successfully created');
         redirect('CR_form/viewMyReports/20');
      }
	}
	/*******************************************************************************
	 *
	 *  Function Name:   sensor_input
	 *
	 *  Date: 2012-04-27
	 *  
	 *  Description: used to create a sensor report
	 *  
	 *  Author: Travis Rous
	 *****************************************************************************/
	public function sensor_input()
	{
	    //Load header of viewincid
        $this->load->helper('dashboard_helper');
        $data['menu'] = getMenu();
		$this->load->helper(array('form', 'url'));
		$this->load->view('headerview',$data);
		
		$this->load->helper(array('form', 'url'));

        //form vaidation
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('sensorID', 'Sensor ID', 'required|integer');
		$this->form_validation->set_rules('Addressline1', 'Address Line', 'required');
		$this->form_validation->set_rules('city', 'City', 'required');
		$this->form_validation->set_rules('State', 'State', 'required');
		$this->form_validation->set_rules('zipcode', 'Zip Code', 'required|integer|min_length[5]|max_length[5]');
		$this->form_validation->set_rules('Comments', 'Comments', 'optional');
		if ($this->form_validation->run() == FALSE)
		{
		    //on failed form validation
			$this->load->view('Sensor_page');
			
		}
		else
		{
		$current_user = $this->session->all_userdata();
		//Success
        $this->load->model('Report_model','report');
        //$current_user = $this->session->all_userdata();
		$this->load->helper('date');
		$datestring = "%Y-%m-%d %H:%i:%s";
		$time = time();
		$form_time = mdate($datestring, $time);
		//create the array for submission
         $submission = array( 
			'SensorID' => $this->input->post('sensorID', TRUE),
			'City' => $this->input->post('city', TRUE),
			'Zipcode' => $this->input->post('zipcode', TRUE),
			'Address' => $this->input->post('Addressline1', TRUE),
			'State' => $this->input->post('State', TRUE),
			'Description' => $this->input->post('Comments', TRUE),
            'CustomerRepID' => $current_user['UserID'],
			'ReportTime' => mdate($datestring, $time),
			'Sensor' => 1
            );
         //submit the report
         $this->report->insert($submission);
		 //set the success message
		 $this->session->set_flashdata('message','Report successfully created');
		 
         // return to the view reports page
         redirect('CR_form/viewMyReports/20');

		}
	}
}
?>
